from django.http import HttpResponse
from django.views.decorators.cache import cache_page
from random import choice
from glob import glob
from nameremoved.settings import MEDIA_ROOT
from nameremoved.random_image.models import Highlight, Title

def index(request):
    return HttpResponse('''<h1>Random Delicious Image</h1>''')

@cache_page(5)
def highlight(request):
    which = Highlight.random()
    f = open(which.get_image_filename())
    return HttpResponse(f.read(), mimetype="image/png")

@cache_page(60)
def title(request):
    which = Title.random()
    filename = which.get_image_filename()
    f = open(which.get_image_filename())
    if "jpg" in filename: mimetype="image/jpeg"
    else: mimetype="image/png"
    return HttpResponse(f.read(), mimetype=mimetype)
